﻿using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

[RequireComponent(typeof(Animator))]
public class Example5 : MonoBehaviour
{
    public AnimationClip clip1;
    public AnimationClip clip2;
    PlayableGraph playableGraph;

    Playable mPlayable;


    void Start()
    {
        var animator = GetComponent<Animator>();

        playableGraph = PlayableGraph.Create();
        mPlayable = AnimationPlayableUtilities.PlayClip(animator, clip1, out playableGraph);

        playableGraph.Play();
    }

    void OnDisable()
    {
        playableGraph.Destroy();
    }

    void OnGUI()
    {
        GUILayout.Box("graph is playing: " + playableGraph.IsPlaying());
        GUILayout.Box("state: " + mPlayable.GetPlayState());
        GUILayout.Box("time: " + mPlayable.GetTime());
        GUILayout.Box("speed: " + mPlayable.GetSpeed());

        if (GUILayout.Button("Play"))
        {
            playableGraph.Play();
        }

        if (GUILayout.Button("Stop"))
        {
            playableGraph.Stop();
        }

        if (GUILayout.Button("evaluate"))
        {
            playableGraph.Evaluate(Time.deltaTime);
        }
    }
}
